package helper

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func ToInt(context *gin.Context, paraName string) int {
	str := context.DefaultQuery(paraName, "-1")
	if str == "" {
		return -1
	}
	res, _ := strconv.Atoi(str)
	return res
}
func ToInt64(context *gin.Context, paraName string) int64 {
	str := context.DefaultQuery(paraName, "-1")
	if str == "" {
		return -1
	}
	res, _ := strconv.ParseInt(str, 10, 64)
	return res
}

func ToDateTime(context *gin.Context, paraName string) *time.Time {
	str := context.DefaultQuery(paraName, "")
	if str == "" {
		return nil
	}
	var layouts = []string{"2006-01-02 15:04:05", "2006-1-2 15:04:05", "2006/1/2 15:04:05"}
	timeVal := time.Time{}
	var err error
	for _, layout := range layouts {
		timeVal, err = time.ParseInLocation(layout, str, time.Local)
		if err == nil {
			break
		}
	}
	if err != nil {
		return nil
	}
	return &timeVal
}

func ToString(context *gin.Context, paraName string) string {
	str := context.DefaultQuery(paraName, "")
	return str
}
